import Validator from "@/utils/Validator"
// 兼容弃用方法
import CACHE from './@deprecated/cache'
/**
 * 异步本地缓存设置
 * @param {*} key 
 * @param {*} value 
 * @param {*} expiresIn 单位是 min
 */
async function setAsync(key, value, expiresIn = null) {
    const data = { value }

    if (expiresIn) {
        data.expires = Date.now() + expiresIn * 60000
    }

    await localStorage.setItem(key, JSON.stringify(data))
}
/**
 * 异步本地缓存读取
 * @param {*} key 
 * @param {*} defaultValue 
 * @returns 
 */
async function getAsync(key, defaultValue = null) {
    const data = await localStorage.getItem(key)

    if (!data) {
        return defaultValue
    }

    const parsedData = JSON.parse(data)

    if (parsedData.expires && parsedData.expires < Date.now()) {
        await localStorage.removeItem(key)
        return defaultValue
    }

    return parsedData.value
}
/**
 * 非异步设置缓存
 * @param {*} key 
 * @param {*} value 
 * @param {*} expiresIn 
 * @returns 
 */
function set(key, value, expiresIn = null){
    const data = { value }
    return new Promise(function(resolve,reject){
        try {
            if (expiresIn) {
                data.expires = Date.now() + expiresIn * 60000
            }
            localStorage.setItem(key, JSON.stringify(data))
            resolve(data)
        } catch (error) {
            reject(error)
        }
    })
}
/**
 * 非异步获取缓存
 * @param {*} key 
 * @param {*} defaultValue 
 * @returns 
 */
function get(key, defaultValue = null) {
    return new Promise(function(resolve,reject){
        try {
            const data = localStorage.getItem(key)
            if(Validator.isEmpty(data)){
                if (Validator.isNotEmpty(defaultValue)){
                    resolve(defaultValue)
                }else{
                    reject('NOT_FOUND')
                }
            }
            const parsedData = JSON.parse(data)
        
            if (parsedData.expires && parsedData.expires < Date.now()) {
                localStorage.removeItem(key)
                return defaultValue
            }
        
            resolve(parsedData.value)
        } catch (error) {
            reject(error)
        }
    })
   
}
// === D === //
/**
 * 异步删除
 * @param {*} key 
 */
async function removeAsync(key){
    await localStorage.removeItem(key)
}
/**
 * 同步删除
 * @param {*} key 
 * @returns 
 */
function remove(key){
    return new Promise(function(resolve,reject){
        try {
            localStorage.removeItem(key)
            resolve(null)
        } catch (error) {
            reject(error)
        }
    })
}
/**
 * 异步清空
 * 
 */
async function clearAsync(){
    await localStorage.clear()
}
function clear(){
    return new Promise(function(resolve,reject){
        try {
            localStorage.clear()
            resolve(true)
        } catch (error) {
            reject(error)
        }
    })
}
export default {
    setAsync,
    getAsync,
    set,
    get,
    removeAsync,
    remove,
    clearAsync,
    clear,
    // @deprecated 弃用方法
    session:CACHE.session,
    local:CACHE.local
}